package simple.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import simple.manager.UserManager;
import simple.manager.UserManagerBean;
import simple.persistence.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class UsersServlet extends HttpServlet {

    private static final long serialVersionUID = 5000309007736480750L;

    private static Log log = LogFactory.getLog(UsersServlet.class);

    private UserManager userManager = new UserManagerBean();

    public void init() throws ServletException {
        super.init();
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.debug("retrieving users");
        List<User> users = userManager.getUsers();

        request.setAttribute("users", users);
        getServletConfig().getServletContext().getRequestDispatcher("/users.jsp").forward(request, response);
    }
}
